package com.umeox.watch.moto.launcher.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.watch.themes.ItemClickListener;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.bean.WatchFaceInfo;

import java.util.List;


/**
 * Created by zl on 2018/7/2.
 */

public class WatchFaceAdapter extends RecyclerView.Adapter<WatchFaceAdapter.ItemViewHolder> {

    private List<WatchFaceInfo> list;
    private ItemClickListener itemClickListener;
    private int mLayoutId;

    public WatchFaceAdapter(List<WatchFaceInfo> list, ItemClickListener itemClickListener, int layoutId) {
        this.list = list;
        this.itemClickListener = itemClickListener;
        mLayoutId = layoutId;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {

        final WatchFaceInfo watchFaceInfo = list.get(position);
        Logger.e(watchFaceInfo.toString());
        holder.mIvBg.setImageResource(watchFaceInfo.getIconResArr()[watchFaceInfo.getIconIndex()]);
        final boolean currentWatchFace = watchFaceInfo.isCurrentWatchFace();
        holder.mIvFg.setVisibility(currentWatchFace ? View.VISIBLE : View.GONE);
        holder.itemView.setOnClickListener(v -> itemClickListener.onItemClicked(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void notifyDataChanged(int currentPosition, int currentIconIndex) {
        Logger.e("currentPosition=" + currentPosition + ",currentIconIndex=" + currentIconIndex);
        list.get(currentPosition).setIconIndex(currentIconIndex);
        Logger.e(list.get(currentPosition).toString());
//        notifyItemChanged(currentPosition);
        notifyDataSetChanged();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIvBg;
        private ImageView mIvFg;

        public ItemViewHolder(View itemView) {
            super(itemView);
            mIvBg = itemView.findViewById(R.id.iv_bg);
            mIvFg = itemView.findViewById(R.id.iv_fg);
        }
    }
}

